<template>
  <div class="w-full" ref="cardRef">
    <div
      class="card"
      :class="{
        [`card--${mode}`]: mode,
        'card--shadow': shadow,
        'card--no-border': !border,
        'card--hoverable': hoverable,
      }"
      :style="{ '--card-height': props.height === 'auto' ? 'auto' : `${state.height}px` }"
    >
      <div
        v-if="$slots.header"
        class="card-header"
        :class="{
          'card--no-border': !border,
        }"
        :style="{ textAlign: headerAlign }"
      >
        <slot name="header"></slot>
      </div>
      <div v-if="$slots.default" class="card-body" :style="{ textAlign: bodyAlign }">
        <slot></slot>
      </div>
      <div v-if="$slots.body" class="card-body" :style="{ textAlign: bodyAlign }">
        <slot name="body"></slot>
      </div>
      <div
        v-if="$slots.footer"
        class="card-footer"
        :class="{
          'card--no-border': !border,
        }"
        :style="{ textAlign: footerAlign }"
      >
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script setup lang="tsx">
import { useElementBounding } from '@vueuse/core';
import { reactive, ref, unref, watch } from 'vue';

interface CardProps {
  mode?: 'default' | 'primary' | 'success' | 'warning' | 'danger';
  shadow?: boolean;
  border?: boolean;
  hoverable?: boolean;
  bodyAlign?: 'left' | 'center' | 'right';
  headerAlign?: 'left' | 'center' | 'right';
  footerAlign?: 'left' | 'center' | 'right';
  height?: string | number;
}
const props = withDefaults(defineProps<CardProps>(), {
  mode: 'default',
  shadow: false,
  border: true,
  hoverable: true,
  bodyAlign: 'left',
  headerAlign: 'left',
  footerAlign: 'left',
  height: 'auto',
});

const cardRef = ref<HTMLDivElement>();
const { height } = useElementBounding(cardRef);

const state = reactive({
  height: 0,
});
watch(
  height,
  () => {
    if (props.height === 'auto') {
      return;
    }
    state.height = height.value;
    console.log(state.height);
  },
  { once: true }
);

const Card = () => {
  return (
    <div ref={cardRef}>
      <div class="card">
        <div class="card-body">
          <slot name="body"></slot>
        </div>
      </div>
    </div>
  );
};
</script>

<style scoped>
.card {
  --card-bg: #fff;
  --card-border: #eee;
  --card-text: #333;
  --card-radius: 5px;
  /* --card-height: calc(v-bind(state.height) * 1px); */

  background: var(--card-bg);
  color: var(--card-text);
  border: 1px solid var(--card-border);
  border-radius: var(--card-radius);
  overflow: hidden;
  transition: box-shadow 0.3s ease, border-color 0.3s ease;
  width: 100%;
  display: flex;
  flex-direction: column;
  /* height: var(--card-height); */
  height: inherit;
}

.card--no-border {
  border: none !important;
}
.card--shadow {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
.card--hoverable {
  transition: box-shadow 0.3s ease;
}
.card--hoverable:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}
.card-header,
.card-body,
.card-footer {
  padding: 20px;
}
.card-header {
  border-bottom: 1px solid #eee;
}
.card-footer {
  border-top: 1px solid #eee;
}
.card-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
}
/* 主题模式 */
.card--primary {
  --card-bg: #f0f7ff;
  --card-border: #b8daff;
  --card-text: #004085;
}

.card--success {
  --card-bg: #f0fff4;
  --card-border: #c3e6cb;
  --card-text: #155724;
}

.card--warning {
  --card-bg: #fffaf0;
  --card-border: #ffeeba;
  --card-text: #856404;
}

.card--danger {
  --card-bg: #fff5f5;
  --card-border: #f5c6cb;
  --card-text: #721c24;
}

.card-header {
  border-bottom: 1px solid var(--card-border);
  font-weight: 600;
  font-size: 1.1em;
}

.card-footer {
  border-top: 1px solid var(--card-border);
  font-size: 0.9em;
}
</style>
